using System.Collections.Generic;

namespace System.Web.Core
{
    public class RoleManager
    {
        private static EntityEngine entityEngine = new EntityEngine();

        public static EntityList GetAllRoles()
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildGetAllRolesRequest()).Reply as EntityList;
        }
        public static Role GetRole(int roleId)
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildGetRoleRequest(roleId)).Reply as Role;
        }
        public static Role GetRole(string name)
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildGetRoleRequest(name)).Reply as Role;
        }
        public static EntityList GetUserRoles(int userId)
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildGetUserRolesRequest(userId)).Reply as EntityList;
        }
        public static bool Create(Role role)
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildCreateRoleRequest(role)).Status == 0;
        }
        public static bool Update(Role role)
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildUpdateRoleRequest(role)).Status == 0;
        }
        public static bool Delete(int roleId)
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildDeleteRoleRequest(roleId)).Status == 0;
        }
        public static bool IsUserInRole(int userId, int roleId)
        {
            return ((EntityList)entityEngine.ExecuteRequest(RequestHelper.BuildGetUserRoleRequest(userId, roleId)).Reply).TotalCount > 0;
        }
        public static bool AddUserToRole(int userId, int roleId)
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildCreateUserRoleRequest(userId, roleId)).Status == 0;
        }
        public static bool RemoveUserFromRole(int userId, int roleId)
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildDeleteUserRoleRequest(userId, roleId)).Status == 0;
        }
        public static bool RemoveUserRoles(int userId)
        {
            return entityEngine.ExecuteRequest(RequestHelper.BuildDeleteUserRolesRequest(userId)).Status == 0;
        }
    }
}